package t4;

import java.util.Calendar;

import t4bean.PurchaseDateBean;
import t4bean.PurchaseOrderBean;
import t4bean.StallBean;
import t4bean.StockBean;

/**
 * 进货逻辑 进已经有的水果自动加到库存 进没有的水果则新建
 *
 */
public class Purchase {

	/**
	 * 将时间存入PurchaseDate中 将fruit存入stock中 存入PurchaseOrder中
	 */
	public StallBean purchase(StallBean stall, String fruitName, Integer cost, Integer price, Integer purchaseQuantity) {
		FruitBean fruit = new FruitBean(fruitName, price, cost);
		Calendar cal = Calendar.getInstance();
		PurchaseOrderBean po = new PurchaseOrderBean(fruit, purchaseQuantity);
		PurchaseDateBean pd = new PurchaseDateBean(fruit, po, cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
		int n = 0;
//		 判断新输入的水果是否存在
		for (StockBean stock : stall.getStockList()) {
			if (stock.getFruit().getName().equals(fruitName)) {
				stock.setStockNumber(stock.getStockNumber() + purchaseQuantity);
				n = 1;
			}
		}
		if (n == 0) {
			StockBean newStock = new StockBean(fruit, purchaseQuantity);
			stall.getStockList().add(newStock);
		}
		stall.getPurDateList().add(pd);
		stall.getPurList().add(po);
		return stall;
	}
}
